<?php
/**
 * Qin functions
 *
 * @package qin
 */

function remove_parent_action() {
  remove_action( 'homepage', 'storefront_homepage_content',      10 );
  remove_action( 'homepage', 'storefront_product_categories',    20 );
  remove_action( 'homepage', 'storefront_on_sale_products',      60 );
  add_action( 'homepage', 'qin_product_categories',    20 );
  add_action( 'homepage', 'qin_shop_features',    15 );

  // remove_action( 'storefront_header', 'storefront_product_search', 40 );
  remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
  // remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
  // add_action( 'woocommerce_after_single_product_summary', 'qin_product_description', 10 );
  // add_action( 'qin_product_description', 'woocommerce_product_description_tab', 10 );
  // add_action( 'qin_product_description', 'woocommerce_product_additional_information_tab', 20 );

  // reorder short description / price
  remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
  add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 7 );
}

// Override the woocommerce_template_loop_product_thumbnail() in woocommerce
function woocommerce_template_loop_product_thumbnail() {
  echo '<div class="product-thumbnail">' . woocommerce_get_product_thumbnail() . '</div>'; // WPCS: XSS ok.
}

function qin_copyright() {
  return 'Copyright &copy;2003-' . date( 'Y' ) . ' HongLei, All Rights Reserved.';
}

function qin_credit_link() {
  return false;
}

function qin_product_description() {
  ?>
  <div class="product-desc">
    <?php
    do_action( 'qin_product_description' );
     ?>
  </div>
  <?php
}

function get_product_categories_json() {
  $terms = get_terms( array(
    'taxonomy' => 'product_cat',
    'hide_empty' => true,
  ) );

  $response = array();
  foreach ( $terms as $term ) {
    $response[] = prepare_product_category_for_response( $term );
  }

  return rest_ensure_response( $response );
}

function prepare_product_category_for_response($item) {
  $data = array(
    'id' => (int) $item->term_id,
    'name' => $item->name,
    'slug' => $item->slug,
    'parent'=> (int) $item->parent,
    'menu_order' => (int) $item->meta_value
  );

  return $data;
}

function qin_enqueue_styles() {
  // Only for homepage
  if( is_front_page() ) {
    wp_enqueue_style( 'swiper', get_stylesheet_directory_uri() . '/assets/sass/vendor/swiper.min.css', false, '4.0.7', 'all' );
  }
}

function qin_enqueue_scripts() {
  /**
   * Scripts
   */
  $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

  wp_enqueue_script( 'qin-menu', get_stylesheet_directory_uri() . '/assets/js/menu' . $suffix . '.js', array( 'jquery', 'underscore' ), '20171006', true );

  // Only for homepage
  // if( is_front_page() ) {
  //   wp_enqueue_script( 'swiper', get_stylesheet_directory_uri() . '/assets/js/vendor/swiper.min.js', array( 'jquery' ), '4.0.7', true );
  //   wp_enqueue_script( 'qin-slider', get_stylesheet_directory_uri() . '/assets/js/slider' . $suffix . '.js', array( 'jquery', 'underscore' ), '20180106', true );
  // }
}


/**
 * Apply inline style to the Qin homepage content.
 *
 * @uses  get_the_post_thumbnail_url()
 * @since  2.2.0
 */
function qin_homepage_content_styles() {
	$featured_image   = get_the_post_thumbnail_url( get_the_ID() );
	$background_image = '';

	if ( $featured_image ) {
		$background_image = 'url(' . esc_url( $featured_image ) . ')';
	}

	$styles = array();

	if ( '' !== $background_image ) {
		$styles['background-image'] = $background_image;
	}

	$styles = apply_filters( 'qin_homepage_content_styles', $styles );

	foreach ( $styles as $style => $value ) {
		echo esc_attr( $style . ': ' . $value . '; ' );
	}
}

if ( ! function_exists( 'qin_header_my_account' ) ) {

  function qin_header_my_account() {
    ?>
    <div class="site-top-navbar">
      <div class="pull-left">OUR CUSTOMER CARE <i class="fa fa-phone-volume"></i> <a href="tel://0086-20-81277640">0086-20-81277640</a></div>
      <ul class="navbar-links pull-right">
        <li>
          <a href="/my-account/">
            <?php
            if ( is_user_logged_in() ) {
              echo 'My Account';
            } else {
              echo 'Sign In';
            }
            ?></a>

          <a href="/my-account/orders/">My Orders</a>
        </li>
      </ul>
    </div>
    <?php
    /*
    <div class="site-my-account">
      <a class="button btn-primary" href="/my-account/">
        <?php
        if ( is_user_logged_in() ) {
          echo 'My Account';
        } else {
          echo 'Sign In';
        }
        ?></a>
    </div>
    */
  }

}

/**
 * Override the storefront_cart_link() in parent theme
 *//*
function storefront_cart_link() {
  ?>
    <a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'storefront' ); ?>"></a>
  <?php
}
*/

function qin_masterslide_before_content() {
  if ( is_front_page() ) {
    masterslider("ms-home");
  }
}


function qin_recent_products_args($args) {
  $args['title'] = 'New Arrivals';
  return $args;
}

function qin_wrap_section_title( $args ) {
  $args['title'] = '<span>' . $args['title'] . '</span>';
  return $args;
}


function qin_remove_review_tab( $tabs ) {
  unset( $tabs['reviews'] );
  return $tabs;
}

function qin_remove_tab_heading( $str ) {
  return '';
}

function qin_product_categories() {
  ?>
  <div class="qin-product-categories clearfix">
    <div class="product-group product-phone">
      <h2>Phone</h2>
      <ul class="product-category qin-product-category clearfix" id="mobile-phone"></ul>
    </div>
    <div class="product-group product-game">
      <h2>Game</h2>
      <ul class="product-category qin-product-category clearfix" id="game"></ul>
    </div>
  </div>
  <?php
}

function qin_shop_features() {
  ?>
  <div class="qin-features-wrap">
    <div class="qin-features clearfix">

      <div class="feature-item">
        <span class="fa fa-shield"></span>
        <h3 class="heading">
          Online Support
        </h3>
        <div class="content">
          Fast online support
        </div>
      </div>

      <div class="feature-item">
        <span class="fa fa-rocket"></span>
        <h3 class="heading">
          Fast Delivery
        </h3>
        <div class="content">
          Best Service for you
        </div>
      </div>

      <div class="feature-item">
        <span class="fa fa-handshake-o"></span>
        <h3 class="heading">
          Free Shipping Options
        </h3>
        <div class="content">
          Free shipping on all orders over $100k
        </div>
      </div>

    </div>
  </div>
  <?php
}


function qin_sidebar_widgets( $args ) {
  $args['after_content'] = array(
    'name'        => __( 'Homepage After Content', 'qin' ),
    'id'          => 'content-last',
    'description' => __( 'Widgets added to this region will appear beneath the content and above the footer.', 'qin' ),
  );

  $rows    = intval( apply_filters( 'qin_home_content_widget_rows', 1 ) );
  $regions = intval( apply_filters( 'qin_home_content_widget_columns', 4 ) );

  for ( $row = 1; $row <= $rows; $row++ ) {
    for ( $region = 1; $region <= $regions; $region++ ) {
      $footer_n = $region + $regions * ( $row - 1 ); // Defines footer sidebar ID.
      $footer   = sprintf( 'home_content_%d', $footer_n );

      if ( 1 == $rows ) {
        $footer_region_name = sprintf( __( 'Homepage Content Column %1$d', 'qin' ), $region );
        $footer_region_description = sprintf( __( 'Widgets added here will appear in column %1$d of the content column.', 'qin' ), $region );
      } else {
        $footer_region_name = sprintf( __( 'Homepage Content Row %1$d - Column %2$d', 'qin' ), $row, $region );
        $footer_region_description = sprintf( __( 'Widgets added here will appear in column %1$d of content column row %2$d.', 'qin' ), $region, $row );
      }

      $args[ $footer ] = array(
        'name'        => $footer_region_name,
        'id'          => sprintf( 'home-content-%d', $footer_n ),
        'description' => $footer_region_description,
      );
    }
  }

  return $args;
}


function qin_content_widget_columns_region() {
  if ( is_front_page() ) {

    $rows    = intval( apply_filters( 'qin_home_content_widget_rows', 1 ) );
    $regions = intval( apply_filters( 'qin_home_content_widget_columns', 4 ) );

    ?><div class="content-column-widget-region">
      <div class="col-full"><?php
    for ( $row = 1; $row <= $rows; $row++ ) :

      // Defines the number of active columns in this footer row.
      for ( $region = $regions; 0 < $region; $region-- ) {
        if ( is_active_sidebar( 'home-content-' . strval( $region + $regions * ( $row - 1 ) ) ) ) {
          $columns = $region;
          break;
        }
      }

      if ( isset( $columns ) ) : ?>
        <div class=<?php echo '"home-content-widgets clearfix row-' . strval( $row ) . ' col-' . strval( $columns ) . ' fix"'; ?>><?php

        for ( $column = 1; $column <= $columns; $column++ ) :
          $footer_n = $column + $regions * ( $row - 1 );

          if ( is_active_sidebar( 'home-content-' . strval( $footer_n ) ) ) : ?>

          <div class="block home-content-widget-<?php echo strval( $column ); ?>">
            <?php dynamic_sidebar( 'home-content-' . strval( $footer_n ) ); ?>
          </div><?php

        endif;
      endfor; ?>

    </div><!-- .home-content-widgets.row-<?php echo strval( $row ); ?> --><?php

      unset( $columns );
    endif;
    endfor;
    ?></div>
    </div><?php
  }

}


function qin_after_content_widget_region() {
  if ( is_front_page() && is_active_sidebar( 'content-last' ) ) {
  ?>
  <div class="content-widget-region" role="complementary">
    <div class="col-full">
      <?php dynamic_sidebar( 'content-last' ); ?>
    </div>
  </div>
  <?php
  }
}


function qin_login_form_scripts() {
  ?><script>
    jQuery(function() {
      var $ = jQuery;

      function toggleForm() {
        $('.login-form-wrap').toggleClass('hide');
        $('.register-form-wrap').toggleClass('hide');
      }

      $('.btn-goto-register').on('click', toggleForm);
      $('.btn-goto-login').on('click', toggleForm);

      $('.entry-header').addClass('hide');
      $('.woocommerce-breadcrumb, .content-area, .site-main, .hentry').css('margin-bottom', 0);
    })
  </script>
  <?php
}
